package com.heima.kafka.listener;

import com.alibaba.fastjson.JSON;
import com.heima.kafka.pojos.Student;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @Author: xrl
 * @Date: 2022/10/19  11:02
 * @Version 1.0
 * 消费者
 */
@Component
public class HelloListener {

    @KafkaListener(topics = "itcast-topic")
    public void onMessage(ConsumerRecord<String, String> record, Consumer<String, String> consumer) {
        if (!StringUtils.isEmpty(record)) {
            System.out.println(record.value());
            System.out.println(record.offset());
            //提交偏移量
            consumer.commitAsync();
        }
    }

    @KafkaListener(topics = "student-topic")
    public void getStudent(String msg) {
        if (!StringUtils.isEmpty(msg)) {
            Student student = JSON.parseObject(msg, Student.class);
            System.out.println(student);
        }

    }

}
